
(function($, window, undefined) {

	function sanitizeQueueParams(params) {
		var defaultQueueParams = {
			queue: '', 
			ack: 0, 
			data: '', 
			db: '', 
			fileName: params.queue + '.default.queue', 
			success: function() { }, 
			error: function() { }
		};
		return $.extend({}, defaultQueueParams, params);
	}
	
	var pymq = {
		addQueue: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/" + params.queue + "/addQueue/", 
				type: "POST", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		dropQueue: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/" + params.queue + "/dropQueue/", 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		},
		loadQueue: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/" + params.queue + "/loadQueue/", 
				data: { fileName: params.fileName }, 
				type: "POST", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		dumpQueue: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/" + params.queue + "/dumpQueue/", 
				data: { fileName: params.fileName }, 
				type: "POST", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		addDB: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/" + params.db + "/addDB/", 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		queueList: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/all/queueList/", 
				type: "GET", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		dbList: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				url: "/_ah/command/all/dbList/", 
				type: "GET", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		rejectAll: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/_ah/command/" + params.queue + "/rejectAll/", 
				type: "POST", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}, 
		rejectConsumers: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/_ah/command/" + params.queue + "/rejectConsumers/", 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		},
		rejectProducers: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/_ah/command/" + params.queue + "/rejectProducers/", 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		resumeQueue: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/_ah/command/" + params.queue + "/resumeQueue/", 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		sendMessage: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/queue/" + params.queue + "/produceMessage/", 
				data: params.data, 
				processData: false, 
				type: "POST", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		receiveMessage: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/queue/" + params.queue + "/consumeMessage/", 
				type: "GET", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		ackMessage: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/queue/" + params.queue + "/none/", 
				type: "GET", 
				dataType: "text", 
				success: params.success, 
				error: params.error
			});
		}, 
		queueStats: function(params) {
			params = sanitizeQueueParams(params);
			$.ajax({
				beforeSend: function(req) {
					if (params.ack) {
						req.setRequestHeader("X-ACKMessage", params.ack);
					}
				}, 
				url: "/queue/" + params.queue + "/stats/", 
				type: "GET", 
				dataType: "json", 
				success: params.success, 
				error: params.error
			});
		}
	};

	window.$pymq = pymq;
}(jQuery, window));
